﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Infragistics.Win.UltraWinListView;
using Xceed.Ftp;
using Xceed.FileSystem;

namespace ResponseMonitor
{
    public partial class ExportForm : Form
    {
        public ExportForm()
        {
            InitializeComponent();
        }

        private void ExportForm_Load(object sender, EventArgs e)
        {
            string[] dirs = Directory.GetDirectories(Settings.Default.SavePath);
            foreach (string d in dirs)
            {
                string dName = d.Substring(d.LastIndexOf("\\") + 1);
                string sName = dName.Substring(dName.IndexOf("_") + 1);
                ultraListViewDirs.Items.Add(new UltraListViewItem(sName, null, null));
            }
        }

        private void ultraButtonExport_Click(object sender, EventArgs e)
        {
            if (ultraListViewDirs.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Please select a project to export!");
                return;
            }

            // 待导出文件信息
            UltraListViewItem sItem = ultraListViewDirs.SelectedItems.First;
            string name = sItem.Text;
            string fName = Settings.Default.SavedDataPrefix + "_" + name;
            string fullName = Settings.Default.SavePath + "\\" + fName;

            // FTP相关参数
            string hostName = Settings.Default.FTPHostName;
            string userName = Settings.Default.FTPUserName;
            string password = Settings.Default.FTPPassword;

            FtpClient client = new FtpClient();
            AbstractFolder localFolder = new DiskFolder(Settings.Default.SavePath);

            // 连接FTP并上传, 此过程需要try-catch
            try
            {
                client.Connect(hostName);
                client.Login(userName, password);
                client.CreateFolder(fName);
                client.ChangeCurrentFolder(fName);
                client.SendMultipleFiles(localFolder.FullName + fName + "\\", true, true);
                client.Disconnect();
            }
            catch (Exception ex)
            {
                Logger.w(ex, "FTP Error");
            }
        }

        private void ultraButtonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
